package cn.wen.core.compress;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;


/**
 * Created by Administrator on 2018/1/10.
 */

public class ImageCache {
    private static volatile ImageCache imageCache;

    public static ImageCache getInstance() {
        if (imageCache == null) {
            synchronized (ImageCache.class) {
                if (imageCache == null) {
                    imageCache = new ImageCache();
                }
            }
        }
        return imageCache;
    }


    private LruCache<String, Bitmap> mCache;

    public ImageCache() {
        // 最大可用内存
        long maxMemory = Runtime.getRuntime().maxMemory() / 1024;
        // 6分之一用来缓存
        long cacheSize = maxMemory / 6;
        mCache = new LruCache<String, Bitmap>((int) cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight() / 1024;
            }
        };
    }

    public void put(String url, Bitmap bitmap) {
        mCache.put(url, bitmap);
    }

    public Bitmap get(String url) {
        return mCache.get(url);
    }


}
